from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from .models import *


class AppTokenObtainPairSerializer(TokenObtainPairSerializer):

    @classmethod
    def get_token(cls, user):
        token = super(AppTokenObtainPairSerializer, cls).get_token(user)
        token['username'] = 'wx_{0}'.format(user.username)
        return token


class WxUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = WxUser
        fields = ['id', 'nick_name', 'avatar_url', 'gender']


class LoginSerializer(serializers.Serializer):
    username = serializers.CharField(required=True)
    password = serializers.CharField(required=True)


class WxSerializer(serializers.Serializer):
    code = serializers.CharField(required=True)
    user_info = serializers.CharField(required=False)


class StaffSerializer(serializers.ModelSerializer):

    class Meta:
        model = Staff
        fields = '__all__'


class ProductSimpleSerializer(serializers.ModelSerializer):
    packages = serializers.ListField(read_only=True)
    brand = serializers.CharField(read_only=True)

    class Meta:
        model = ProductName
        exclude = ['created_by', 'updated_by', 'is_oem', 'is_active', 'related_name', 'product_category']


class ProductBatchSerializer(serializers.ModelSerializer):

    class Meta:
        depth = 0
        model = ProductBatch
        exclude = ['created_by', 'updated_by']


class MaterialPublicSerializer(serializers.ModelSerializer):

    class Meta:
        depth = 1
        model = Material
        exclude = ['created_by', 'updated_by', 'total_in', 'left_amount', 'supplier', 'is_active', 'is_public']
